home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / getcwd.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  1KB  |  63 lines

  1. #include <compiler.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>    /* both of these added for malloc() */
  4. #include <limits.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <osbind.h>
  8. #include <mintbind.h>
  9. #include <unistd.h>
  10. #include "lib.h"
  11.  
  12. /*******************************************************************
  13. getcwd: returns current working directory. By ERS.
  14. This routine is in the public domain.
  15. ********************************************************************/
  16.  
  17. extern int __mint;
  18. extern char _rootdir;    /* in main.c: user's preferred root directory */
  19.  
  20. char *getcwd(buf, size)
  21. char *buf; int size;
  22. {
  23.     char _path[PATH_MAX];
  24.     char *path;
  25.     char drv;
  26.     int buf_malloced = 0;
  27.     int r;
  28.  
  29.     if (!buf) {
  30.         if ((buf = (char *) malloc((size_t)size)) == 0)
  31.             return NULL;
  32.         buf_malloced = 1;
  33.     }
  34.  
  35.     drv = Dgetdrv() + 'a';
  36.     _path[0] = drv;
  37.     _path[1] = ':';
  38.     _path[2] = '\0';
  39.     path = _path + 2;
  40.     if (__mint >= 96) {
  41.         if ((r = (int) Dgetcwd(path, 0, size - 2)) != 0) {
  42.             if (buf_malloced)
  43.                 free(buf);
  44.             errno = -r;
  45.             return NULL;
  46.         }
  47.     } else {
  48.         (void)Dgetpath(path, 0);
  49.     }
  50.  
  51.     if (_rootdir && drv == _rootdir) {
  52.         if (!*path) {
  53.             path[0] = '\\';
  54.             path[1] = '\0';
  55.         }
  56.         _dos2unx((char *)path, buf);
  57.         return buf;
  58.     }
  59.     _dos2unx(_path, buf);    /* convert DOS filename to unix */
  60.     return buf;
  61. }
  62.  
  63.